package com.zzx.threekingdom.view;

import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.card.CardList;
import com.zzx.threekingdom.card.CharacterCard;
import com.zzx.threekingdom.card.EquipmentCard;
import com.zzx.threekingdom.judge.Judge;
import com.zzx.threekingdom.model.AbsPlayerModel;
import com.zzx.threekingdom.model.ActionInfo;
import com.zzx.threekingdom.model.AbsPlayerModel.JudgeListener;
import com.zzx.threekingdom.resmanager.ResManager;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class PlayerView extends ViewGroup implements CardList.CardListListener, OnLongClickListener, JudgeListener
{
    public interface PressListener { void onPressed(PlayerView playerView); }
    
    public CardView characterView;
    public PlayerEquipmentView equipmentView;
    public HealthView healthView;
    private CardList mCards = null;
    private PressListener mPressListener = null;
    private NotifyHandler mNotifyHandler = new NotifyHandler() { public void onNotify() {
        if(mPressListener != null) mPressListener.onPressed(PlayerView.this);
    } };
    private boolean setLeaveAllBehind = false;
    private boolean setLightning = false;
    
    public PlayerView(Context context)
    {
        this(context, null);
    }

    public PlayerView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
    public PlayerView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        addView(healthView = new HealthView(context));
        addView(characterView = new CardView(context));
        addView(equipmentView = new PlayerEquipmentView(context));
        
        characterView.setClickable(true);
        characterView.setLongClickable(true);
        characterView.setOnLongClickListener(this);
        setPadding(2, 2, 2, 2);
    }
    public void setCards(CardList cards) { mCards = cards; }
    public void onCardsChanged(CardList cards) { invalidate(); }
    public CardView getCharacterView() { return characterView; }
    public PlayerEquipmentView getEquipmentView() { return equipmentView; }
    public HealthView getHealthView() { return healthView; }
    public void addEquipment(EquipmentCard card) { equipmentView.addEquipment(card); }
    public void setCharacter(CharacterCard card) { characterView.setCard(card); }
    public void setListener(PressListener listener) { mPressListener = listener; }
    public void notifyListener() { mNotifyHandler.notifyListener(); }
    public boolean isSelected() { return characterView.isPressed(); }
    

    public boolean onLongClick(View v)
    {
        Card characterCard = characterView.getCard(); 
        if(characterCard!=null)
        {
            String hint = characterCard.getHelpText();
            if(hint!=null&&hint.length()!=0)
                new HintToast(getContext()).showHintText(characterView, hint, Gravity.CENTER, 0, 0, HintToast.DEFAULT_DELAYMS);
        }

        return true;
    }
    
    public void setSelected(boolean selected)
    {
        checkDead();
        if((isDead()==false) && (characterView.isPressed()!=selected))
        {
            characterView.setPressed(selected);
            characterView.invalidate();
            notifyListener();
        }

    }
    
    public void setCanSelected(boolean canSelected)
    {
        checkDead();
        if(isDead()==false)
            setEnabled(canSelected);
    }
    
    private boolean isDead()
    {
        AbsPlayerModel pm = Judge.judge.getModelByPlayerView(this);
        return (pm!=null)?pm.isDead() : false;
    }
    
    private boolean isJudge(String name)
    {
        AbsPlayerModel pm = Judge.judge.getModelByPlayerView(this);
        return (pm!=null) ? (pm.getJudgeByName(name)!=null) : false;
    }
    
    public void checkDead()
    {
        if(isDead()==true)
        {
            if(characterView.isPressed() == true)
            {
                characterView.setPressed(false);
                characterView.invalidate();
            }
            setEnabled(false);
        }
    }
    
    public void clearView()
    {
        characterView.clearView();
        equipmentView.clearView();
        healthView.clearView();
        setEnabled(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        healthView.measure(freeSpec, freeSpec);
        characterView.measure(freeSpec, freeSpec);
        equipmentView.measure(freeSpec, freeSpec);
        
        setMeasuredDimension(Math.max(healthView.getMeasuredWidth(), characterView.getMeasuredWidth()) + equipmentView.getMeasuredWidth()
                , Math.max(healthView.getMeasuredHeight() + characterView.getMeasuredHeight(), equipmentView.getMeasuredHeight()));
    }
    @Override
    public void addView(View child, int index, LayoutParams params)
    {
        if((child==healthView)||(child==characterView)||(child==equipmentView))
        {
            super.addView(child, index, params);
        }
        
    }
    @Override
    protected void onLayout (boolean changed, int l, int t, int r, int b)
    {
        int midHC = (healthView.getMeasuredHeight() + characterView.getMeasuredHeight())/2;
        int midLineY = Math.max(midHC, equipmentView.getMeasuredHeight()/2);
        int healthTop = getPaddingTop() + midLineY - midHC;
        int healthRight = getPaddingLeft() + healthView.getMeasuredWidth();
        int healthBottom = healthTop + healthView.getMeasuredHeight();
        healthView.layout(getPaddingLeft(), healthTop, healthRight,healthBottom);
        characterView.layout(getPaddingLeft(), healthBottom, getPaddingLeft() + characterView.getMeasuredWidth()
                , healthBottom + characterView.getMeasuredHeight());
        
        int weaponLeft = Math.max(healthView.getMeasuredWidth(), characterView.getMeasuredWidth());
        int weaponTop = getPaddingTop() + midLineY - equipmentView.getMeasuredHeight()/2;
        equipmentView.layout(weaponLeft, weaponTop, weaponLeft + equipmentView.getMeasuredWidth()
                , weaponTop + equipmentView.getMeasuredHeight());
    }
    @Override
    protected void dispatchDraw(Canvas canvas)
    {
        super.dispatchDraw(canvas);
        if(mCards!=null)
        {
            Paint p = new Paint();
            p.setColor(Color.WHITE);
            Rect bounds = new Rect();
            String cardNum = mCards.size() + "";
            p.getTextBounds (cardNum, 0, cardNum.length(), bounds);
            RectF newBounds = new RectF(0, getMeasuredHeight() - bounds.height(), bounds.width(), getMeasuredHeight());
            canvas.drawRect(newBounds, p);
            p.setColor(Color.RED);
            canvas.drawText(cardNum, newBounds.left, newBounds.bottom, p);
        }
        
        if(!((isDead()==false)&&(isEnabled()==true)))
        {
            canvas.drawARGB(125, 0, 0, 0);
        }
        
        if(isJudge("乐不思蜀")==true)
        {
            canvas.drawText("乐", 0, 0, null);
            
        }
        if(isJudge("闪电")==true)
        {
            canvas.drawText("电", 20, 0, null);
            
        }
        
    }
    
    public void onJudgeChanged(ArrayList<ActionInfo> judgeArea) { invalidate(); }
    



}
